<?php

/**
 * 用户令牌 Session缓存逻辑
 * Created by PhpStorm.
 * @author 清凉Tea <973110519@qq.com>
 * Date: 2018/1/9
 * Time: 19:15
 */

namespace app\api\service;

use think\Db;
use think\Cache;

class TokenService
{
    public $userId;
    public $openId;
    public $userInfo;
    protected $available = false;

    protected static $options ;

    public function __construct()
    {
        self::$options = [
            // 缓存类型为File
            'type' => 'File',
            // 缓存有效期为永久有效
            'expire' => 0,
            // 指定缓存目录
            'path' => RUNTIME_PATH .'token/'
        ];
    }


    public function check($token)
    {
        $userInfo = cache($token,'',self::$options);
        if (!empty($token) && $userInfo != null) {
            $this->userId = $userInfo['user_id'];
            $this->openId = $userInfo['openid'];
            $this->userInfo = $userInfo;
            $this->available = true;
        }
        return $this->available;
    }

    /**
     *  创建用户Session 会话
     * @author 清凉Tea <973110519@qq.com>
     */
    public function cacheSession($oldToken,$data)
    {
        cache($oldToken,null,self::$options);
        cache($data['token'],$data,self::$options);
    }
}